<?php
/**
* @nameMaiji
* @authorpd87@qq.com
* @copyrightm-j.cc
* @versionv1.0
*/
namespace MAIJI;

class APP_Error {
	
	static $info=array();
	static $sql=array();
	static $msg = array(
		E_WARNING=>'运行时警告',
		E_NOTICE=>'运行时提醒',
		E_STRICT=>'编码标准化警告',
		E_USER_ERROR=>'自定义错误',
		E_USER_WARNING=>'自定义警告',
		E_USER_NOTICE=>'自定义提醒',
		'Unkown '=>'未知错误'
	);
	
	//初始化方法
	public static function start(){
		// ini_set("display_errors", "off");
		error_reporting(E_ALL);
		//处理Warning和notice级别错误
		set_error_handler(array(__CLASS__,'appError'));
		//捕捉致命错误,脚本终止 就触发
		register_shutdown_function(array(__CLASS__,'appFatalError'));
		//自动捕获异常
		set_exception_handler(array(__CLASS__,'appException'));
	}
	
	//错误处理回掉函数
	public static function appError($errno,$errstr,$errfile,$errline){
		$error = array();
		$error['type'] = $errno;
		$error['message'] = $errstr;
		$error['file'] = $errfile;
		$error['line'] = $errline;
		$error['msg'] = self::addcode($error);
		self::addmsg($error);
		self::display();
	}
	
	
	//致命错误处理
	public static function appFatalError(){
		if(!is_null($error = error_get_last()) && in_array($error['type'],[E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR,E_USER_ERROR])){
			self::echoerror($error);
		}
	}
	
	//自动捕获异常
	public static function appException($e){
		$error = array();
		$error['type'] = $e->getCode();
		$error['message'] = $e->getMessage();
		$error['file'] = $e->getFile();
		$error['line'] = $e->getLine();
		self::echoerror($error);
	} 
	
	// 输出错误
	public static function echoerror($error){
		if($error['type'] == '2002'){
			$error['message'] = iconv('gbk', 'utf-8', $error['message']);
		}
		$ls = self::my();
		ob_end_clean();
		if(!C('basic.DEBUG')){
			\MAIJI\Log::x($error,'error');
			$error['file'] = '这个位置怎么能告诉你呢! 嘿嘿!';
		}
		header('HTTP/1.0 404 NOT FONUND');
		echo '<h1 style="font-size:100px;">:(</h1><br/>';
		echo "<h3>{$error['type']} : {$error['message']}</h3>";
		echo '<br><h4>错误位置：</h4>';
		echo "<p>{$error['file']}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$error['line']}</p><hr>";
		echo '<h4>PhpSmarty <a href="#" style="color:#000">v1.0</a>&nbsp;&nbsp;&nbsp;&nbsp;{ 为API生，为API死，为API奋斗一辈子！ }</h4><hr>';
		echo $ls[0].'</br></br>'.$ls[1];
		die;
	}
	
	//输出自己的错误
	public static function display($msga=null,$msgb=null){
		if(!is_null($error = error_get_last())){
			self::echoerror($error);
		}elseif($msga !== null || $msgb !== null){
			$error['type'] = '999';
			$error['line'] = '';
			$error['message'] = $msga;
			$error['file'] = $msgb;
			self::echoerror($error);
		}
		if(isset(self::$info) && count(self::$info) > 0){
			foreach(self::$info as $error){
				$_html = '［'.$error['msg'].'］-> 错误标识：['.$error['type'].'] -> 错误信息：['.$error['message'].'] -> 错误位置：';
				$_html .= '['.$error['file'].' '.$error['line'].' 行];';
				$GLOBALS['_DEBUG']['error'][] = $_html;
			}
		}

		if(isset(self::$sql) && count(self::$sql) > 0) {
			foreach(self::$sql as $error){
				$GLOBALS['_DEBUG']['mysql'][] = ' [ Error ] '.$error;
			}
		}
	}
	
	// 随机名言
	public static function my(){
		$ls = 
		[
			[
				'懒惰：</br>是这样一种品质，它使得你花大力气去避免消耗过多的精力。它敦促你写出节省体力的程序，同时别人也能利用它们。为此你会写出完善的文档，以免别人问你太多问题。',
				"Laziness:</br>The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful, and document what you wrote so you don't have to answer so many questions about it. Hence, the first great virtue of a programmer, Also hence, this book."
			],[
				'急躁：</br>是这样一种愤怒——当你发现计算机懒洋洋地不给出结果。于是你写出更优秀的代码，能尽快真正的解决问题。至少看上去是这样。',
				"Impatience:</br>The anger you feel when the computer is being lazy. This makes you write programs that don't just react to your needs, but actually anticipate them. Or at least pretend to. Hence, the second great virtue of a programmer."
			],[
				'傲慢：</br>极度的自信，使你有信心写出（或维护）别人挑不出毛病的程序。',
				"Hubris:</br>Excessive pride, the sort of thing Zeus zaps you for. Also the quality that makes you write (and maintain) programs that other people won't want to say bad things about. Hence, the third great virtue of a programmer."
			],
		];
		return $ls[rand(0,count($ls)-1)];
	}
	
	public static function addmsg($msg,$type=0) {
		switch($type){
			case 0:
				self::$info[]=$msg;
				break;
			case 1:
				self::$sql[]=$msg;
				break;
		}
	}
	
	public static function addcode($error){
		if(isset(self::$msg[$error['type']])){
			return self::$msg[$error['type']];
		}else{
			self::echoerror($error);
			return '未知错误';
		}
	}
}